package mi.payment.utils.iso8583;

import android.os.RemoteException;

import com.sunmi.pay.hardware.aidl.AidlConstants;
import com.sunmi.pay.hardware.aidl.bean.EmvTermParam;
import com.sunmi.pay.hardware.aidl.emv.EMVOpt;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;

import mi.payment.MyApplication;
import mi.payment.utils.Config;
import sunmi.sunmiui.utils.LogUtil;

public class ImportRidAndAidParamsUtil {

    private static final String TAG = "ImportRidAndAidParamsUtil";

    public static void clean() {
        try {
            MyApplication.getInstance().emvOpt.updateAID(AidlConstants.EMV.AID.ACTION_AID_DEL, null);
            MyApplication.getInstance().emvOpt.updateCAPK(AidlConstants.EMV.CAPK.ACTION_CAPK_DEL, null);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public static void setParam(EmvTermParam param) {
        try {
            MyApplication.getInstance().emvOpt.setTerminalParam(param);
            MyApplication.getInstance().emvOpt.syncParam();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    /**
     * 设置RID
     */
    public static void setRIDs() {
        LogUtil.e(TAG, "setRIDs");
        Properties RIDs = Config.loadRIDs();
        if (RIDs == null) {
            return;
        }
        EMVOpt emvOpt = MyApplication.getInstance().emvOpt;
        Enumeration<Object> enu = RIDs.elements();
        List<String> list = new ArrayList<>();
        while (enu.hasMoreElements()) {
            String value = enu.nextElement().toString();
            list.add(value);
        }
        try {
            emvOpt.insertCAPK(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 设置AID
     */
    public static void setAIDs() {
        LogUtil.e(TAG, "setAIDs");
        Properties AIDs = Config.loadAIDs();
        if (AIDs == null) {
            return;
        }
        EMVOpt emvOpt = MyApplication.getInstance().emvOpt;
        Enumeration<Object> enu = AIDs.elements();
        List<String> list = new ArrayList<>();
        while (enu.hasMoreElements()) {
            String value = enu.nextElement().toString();
            list.add(value);
        }
        try {
            emvOpt.insertAID(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
